/*=============================================================================
*
*   Copyright (C) 2020 All rights reserved.
*
*   Filename:		data_parser.hpp
*
*   Author: Wang Zhecheng - wangzhecheng@yeah.net
*
*   Date: 2020-02-14 03:58
*
*   Last Editors: Wang Zhecheng - wangzhecheng@yeah.net
*
*   Last modified:	2020-03-20 16:22
*
*   Description:
*
=============================================================================*/
#ifndef DATAREADER_H
#define DATAREADER_H

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <filesystem>

namespace geoxfem {
namespace io {

struct json {};
struct xml {};

template <typename InputFormat>
struct DataParser {
  using ptree = boost::property_tree::ptree;

  DataParser() : m_ptree() {}
  DataParser(const ptree& _ptree) : m_ptree(_ptree) {}

  void read(const std::string& relative_path) {
    m_path = std::filesystem::current_path().append(relative_path);
    boost::property_tree::read_json(m_path, m_ptree);
    return;
  }
  //////////////////////////////////////////////////////////////////////

  std::filesystem::path getPath() const {
    std::filesystem::path p = m_path;
    return p.remove_filename();
  }
  std::filesystem::path getFileName() const {
    return m_path.stem();
  }
  //////////////////////////////////////////////////////////////////////

  ptree& data() {
    return m_ptree;
  }

  const ptree& data() const {
    return m_ptree;
  }
  bool hasValue(const std::string& path) const {
    return m_ptree.get_child_optional(path) ? true : false;
  }
  //////////////////////////////////////////////////////////////////////

  ptree& getMaterials() {
    return m_ptree.get_child("materials");
  }
  ptree& getPhases() {
    return m_ptree.get_child("phases");
  }
  ptree& getTopology() {
    return m_ptree.get_child("topology");
  }
  ptree& getCrack() {
    return getTopology().get_child("cracks");
  }
  //////////////////////////////////////////////////////////////////////

  std::string getProjName() const {
    return m_ptree.get<std::string>("description.name");
  }
  std::filesystem::path getOutputPath() const {
    std::string           path = m_ptree.get<std::string>("io.output.path");
    std::filesystem::path p    = std::filesystem::current_path();
    if(path.empty())
      p.append("result");
    else
      p.append(path);
    p.append(getProjName());

    return p;
  }

private:
  ptree                 m_ptree;
  std::filesystem::path m_path;
};

}  // namespace io
}  // namespace geoxfem

#endif /* DATAREADER_H */
